package com.example.demo1.generator.codedesign;

import org.apache.commons.lang3.StringUtils;


public class CommonUtil {

	/**
	 * <p>
	 * 将下划线"_"从字符串中删除，其后的第一个字母变大写
	 * </p>
	 *
	 * <pre>
	 * underlineToCapitalize(null)  = null
	 * underlineToCapitalize("name_of_abc")    = "nameOfAbc"
	 * </pre>
	 *
	 * @param str
	 * @return 返回格式后的字符串或 <code>null</code>
	 * @since 1.0
	 */
	public static String underlineToCapitalize(String str) {
		if (str == null) {
			return null;
		}
		String[] words = str.split("_");
		StringBuilder sb = new StringBuilder();
		sb.append(words[0]);
		for (int i = 1; i < words.length; i++) {
			sb.append(StringUtils.capitalize(words[i]));
		}
		return sb.toString();
	}
	
	/**
	 * 输入一个字符串数组，返回拼接好的大写串
	 * @param strs
	 * @return
	 */
	public static String arrayToCapitalize(String[] strs) {
		if (strs == null || strs.length == 0) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		for (String s : strs)
			sb.append(StringUtils.capitalize(s));
		return sb.toString();
	}

	/**
	 * <p>
	 * 大写字母前加下划线"_",首字母大写前不拼接
	 * </p>
	 * 
	 * <pre>
	 * capitalizeToUpperUnderline("residentId")  = "RESIDENT_ID"
	 * capitalizeToUpperUnderline("oah")    = "OAH"
	 * capitalizeToUpperUnderline("Code")    = "CODE"
	 * </pre>
	 * 
	 * @param str
	 * @return
	 */
	public static String capitalizeToUpperUnderline(String str) {
		if (str == null) {
			return null;
		}
		StringBuilder sb = new StringBuilder();
		char[] chars = str.toCharArray();
		String split = "_";
		for (int i = 0; i < chars.length; i++) {
			char item = chars[i];
			if (Character.isUpperCase(item)) {
				if (i > 0 && i < (chars.length - 1)) {
					sb.append(split);
				}
			}
			sb.append(item);
		}
		return sb.toString().toUpperCase();
	}
	
}
